package com.itheima.stock.face.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.stock.constant.PermissionConstant;
import com.itheima.stock.face.StockBackendCacheFace;
import com.itheima.stock.mapper.SysPermissionMapper;
import com.itheima.stock.mapper.SysRoleMapper;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.mapper.SysUserRoleMapper;
import com.itheima.stock.pojo.domain.SysUserDomain;
import com.itheima.stock.pojo.domain.UserDomain;
import com.itheima.stock.pojo.dto.PermissionsDTO;
import com.itheima.stock.pojo.entity.SysRole;
import com.itheima.stock.pojo.vo.req.PageResult;
import com.itheima.stock.pojo.vo.req.UserPage;
import com.itheima.stock.pojo.vo.resp.PermissionsVO;
import com.itheima.stock.pojo.vo.resp.R;
import com.itheima.stock.pojo.vo.resp.ResponseCode;
import com.itheima.stock.process.DataProcess;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Component
@CacheConfig(cacheNames = "user")
public class StockBackendCacheFaceImpl implements StockBackendCacheFace {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private DataProcess dataProcess;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Cacheable(key = "#root.method.getName()+':'+#userId")
    @Override
    public List<String> getRole(Long userId) {
        List<SysRole> roles = sysRoleMapper.getRoleByUserId(userId);
        List<String> role = roles.stream().map(r -> "ROLE_" + r.getName()).collect(Collectors.toList());
        return role;
    }
    @Cacheable(key = "#root.method.getName()+':'+#userId")
    @Override
    public List<String> getCode(Long userId) {
        List<PermissionsDTO> permissions = sysPermissionMapper.getPermissionsDTOByUId(userId);
        List<String> code = permissions.stream().filter(per -> !StringUtils.isBlank(per.getCode())).
                map(per -> per.getCode()).collect(Collectors.toList());
        return code;
    }
    @Cacheable(key = "#root.method.getName()+':'+#userId")
    @Override
    public List<String> getPerms(Long userId) {
        List<PermissionsDTO> permissions = sysPermissionMapper.getPermissionsDTOByUId(userId);
        List<String> perms = permissions.stream().filter(per -> !StringUtils.isBlank(per.getPerms())).
                map(per -> per.getPerms()).collect(Collectors.toList());
        return perms;
    }

    @Cacheable(key = "#root.method.getName()+':'+#userId")
    @Override
    public List<PermissionsVO> getPermissionsVO(Long userId) {
        List<PermissionsDTO> permissions = sysPermissionMapper.getPermissionsDTOByUId(userId);
        List<PermissionsVO> permissionVOs = dataProcess.getMenus(permissions, PermissionConstant.TOP_PERMISSION);
        return permissionVOs;
    }

}
